﻿$(function () {
    $('#userName').focus();
    $('#pwd').keyup(function (e) {
        var userName = $('#userName').val();
        var pwd = $('#pwd').val();
        if (userName.length > 0 && pwd.length > 0) {
            $('#ErrorMsg').html('').hide();
        }
        else {
            $('#ErrorMsg').html('用户名密码不能为空').show();
        }
    });
    $(document).keyup(function (e) {
        if (e.keyCode == 13 && validate()) {
            loginIn();
        }
    });
    $('#btnLogin').click(function () {
        if (validate()) {
            loginIn();
        }
    });

});

var loginIn = function () {
    $.ajax({
        url: '/PageHandler.ashx',
        dataType: 'JSON',
        type: 'POST',
        data:
        {
            cn: 'User',
            mn2: 'LoginIn',
            userName: $('#userName').val(),
            pwd: $('#pwd').val()
        },
        success: function (twiReturn, textStatus, jqXHR) {
            if (twiReturn.success) {
                $.cookie('twi_scs_cookie_FRealName',twiReturn.data.FRealName);
                location.href = "Home.aspx";
            }
            else {
                $('#ErrorMsg').html(twiReturn.errors).show();
            }
        }
    })
}

var validate = function () {
    var userName = $('#userName').val();
    var pwd = $('#pwd').val();
    if (userName.length > 0 && pwd.length > 0) {
        $('#ErrorMsg').html('').hide();
        return true;
    }
    else {
        $('#ErrorMsg').html('用户名密码不能为空').show();
        return false;
    }
}